import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import Goodshearder from './goodshearder'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import '../my/unpaid.scss'
import './cloud.scss'

export default class Index extends Component {
  constructor(props) {
  	super(props);
    this.state = {
    	goodslist:[],
      pageheight:0,
	  	pageNumber:0,//页码数
      scrollPd:false,//是否可滑动
      hasNextPage:false,//是否有下一页
    }
  }
  config = {
    navigationBarTitleText: '云采购订单'
  }

  componentWillMount () {
  	var _this=this
  	Taro.getSystemInfo({
      success:function(res){
        _this.setState({
          pageheight: res.windowHeight
        })
      }
    })
		this.getdata(0)
  }
  componentDidMount () {}
  componentDidShow () {}
  getdata(pageNumber){
  	 var _this=this;
  	 var dateinfo = Taro.getStorageSync('dateinfo');
  	 var url='/yunwarehouse/papi/v1.9/ticket?page='+pageNumber+"&toDealerId.equals="+dateinfo.userid+'&type.equals=PURCHASE';
    CommonJs.ajaxRequest(url,'','GET',function(res){
		if(res.data.page<res.data.totalPages){
			var hasNextPage = true
		 }
		 else{
			var hasNextPage = false
		 }
		  var results = _this.state.goodslist
		  var result = res.data.list;
		  for(var i = 0; i < result.length; i++){
			results.push(result[i])
		  }
		  if (hasNextPage) {
			_this.setState({
			  goodslist:results,
			  pageNumber: res.data.page,
			  hasNextPage: hasNextPage,
			  scrollPd: true
			})
		  }
		  else {
			_this.setState({
			  goodslist:results,
			  pageNumber: res.data.page,
			  hasNextPage: hasNextPage,
			  scrollPd: false
			})
		  }

    },'wfw')
  }
  loadMore(){
    var _this = this;
    var pd = _this.state.scrollPd;
    if(pd) {
      //防止多次触发滑动到底部事件
      _this.setState({
        scrollPd: false//禁止下拉
      })
      var pageNumber = _this.state.pageNumber + 1;
      _this.getunpaid(pageNumber)
    }else{
      var hasNextPage =  _this.state.hasNextPage;
      if(hasNextPage){
        Taro.showToast({
            title:'正在加载数据...',
            icon: 'none',
            duration: 2000
          });
      }else{
        Taro.showToast({
            title:'没有更多数据啦！',
            icon: 'none',
            duration: 2000
          });
      }
    }
  }
   ordertype(val){
	  if(val=='STAY'){
		  return '待发货'
	  }
	  else if(val=='APPLY'){
		  return '申请(通知)发货'
	  }
	  else if(val=='COMPLETE'){
		  return '发货完成'
	  }
	  else{
		  return ''
	  }
  }
  open(url){
    CommonJs.open(url);
  }
    render () {
    const result = this.state.goodslist
    return (
      <View className='page'>
        <View className='qun-bgbanner'>
          	<View className='change'>
	        		<View className='change-li change-licur' onClick={this.open.bind(this,'/pages/cloud/cgorder')}>采购订单<Text className='xz'></Text></View>
	        		<View className='change-li' onClick={this.open.bind(this,'/pages/cloud/cggoods')}>采购商品<Text className='xz'></Text></View>
	        	</View>
        </View>
        <ScrollView className='qun-goods' scrollY='true' style={'height:'+this.state.pageheight+'px;margin-top:90rpx'} onScrollToLower={this.loadMore.bind(this,'')}>
        <View className='qun-znwn' style={result.length!=0?'display:none':'display:block;'}>暂无订单</View>
        {result.map((item, index) => (
        <View className='goodsorder-content'>
          <View className='goodsorder-unpaid'>
            <View className='unpaid-title'>
              <View className='qun-scallty'>
				<View className='unpaid-state'>{this.ordertype(item.sentStatus)}</View>
                <Image mode='scaleToFill' className='unpaid-photo' src='https://minichat.g2ma.com/kedouimg/store.png'></Image>
                <View className='unpaid-shopname'><Text className='unpaid-tel'>{item.fromShopName}</Text>({item.orderSerialNo})</View>
              </View>
            </View>
            <View className='unpaid-content'>
              {item.goodsNoTicketVMList.map((item1, index1) => (
              <View className='unpaid-goods'>
							<View className='unpaid-pic'>
								<Image mode='scaleToFill' className='unpaid-pic-img' src={item1.itemPicture?item1.itemPicture:'https://minichat.g2ma.com/kedouimg/A6984900727459logo.png'}></Image>
			                </View>
							<View className='unpaid-right'>
			                  <View className='unpaid-goodsname'>{item1.itemName}</View>
			                  <View className='unpaid-specifications'>规格：{item1.itemSku}</View>
			                  <View className='unpaid-total'>
			                    <View className='unpaid-num'>数量：{item1.amount}</View>
			                    <View className='unpaid-price'>{item1.price/100}{this.state.malltype?this.state.malltype:'元'}</View>
			                    
			                  </View>
			                </View>
			              </View>
						  ))}
              
              <View className='unpaid-postage'>总数量：{item.goodsAmount}
              <View className='unpaid-time' style='float:left;text-aling:left;'>{CommonJs.FormatDate(item.createAt,3)}</View>
              </View>

            </View>
           
           
          </View>
        </View>
        ))}
        </ScrollView>
      </View>
    )
  }
}